package com.whl.sm.controller.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.model.parking.ParkingRecord;
import com.whl.sm.service.parking.ParkingLotsService;
import com.whl.sm.service.parking.ParkingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/parking-record")
public class ParkingRecordController extends BaseController {

    @Autowired
    private ParkingLotsService parkingLotsService;


    @Autowired
    private ParkingRecordService parkingRecordService;

    @RequestMapping("/list")
    public Result getParkingLots(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("license_plate");
        setHttpRequestQueryWrapper(queryWrapper,"visible","parking_lots_id");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("visible");
        Page<Object> recordPage = parkingRecordService.page(getPage(),queryWrapper);
        recordPage.getRecords().stream().forEach(obj->{
            ParkingRecord parkingRecord = (ParkingRecord) obj;
            ParkingLots parkingLots = parkingLotsService.getById(parkingRecord.getParkingLotsId());
            parkingRecord.setParkingLotsCode(parkingLots.getParkingCode());
        });

        return Result.ok(recordPage);
    }



    @RequestMapping("/mentalOperate")
    public Result mentalOperate (@RequestParam("parkingLotsId") String parkingLotsId, @RequestParam("licensePlate") String licensePlate,@RequestParam("operate") int operate){
        return parkingRecordService.addRecord(parkingLotsId,licensePlate,operate);
    }


    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            parkingRecordService.update(new UpdateWrapper<ParkingRecord>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }


}
